﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MedManager.BLL;
using Maticsoft.DBUtility;
using System.Data.SqlClient;
using MedManager.Model;
using Maticsoft.Common;
using MedManager.Properties;
using MedManager.Src;
using MessageBox = System.Windows.Forms.MessageBox;

namespace MedManager
{
    public partial class Login : Form
    {
        #region Porperty:

        private string UserName
        {
            get
            {
                return txtUserName.Text;
            }
        }

        private string Password
        {
            get
            {
                return txtPassword.Text;
            }
        }

        userinfoEntity user = new userinfoEntity();
        #endregion
        #region Function:
        /// <summary>
        /// 检查登录
        /// </summary>
        /// <returns></returns>
        private bool CheckLogin()
        {
            bool flag = false;
            userinfoBLL userbll = new userinfoBLL();
            string sql = "select * from userinfo where username=@username and password=@password and status=0";
            SqlParameter[] parameter = new SqlParameter[2];
            parameter[0] = new SqlParameter("@username", UserName);
            parameter[1] = new SqlParameter("@password", Password);
            //DbHelperSQL.connectionString = Settings.Default.ConnectionString;
            DataSet ds = DbHelperSQL.Query(sql, parameter);
            if (ds.Tables[0].Rows.Count > 0)
            {
                flag = true;
                user = userbll.GetModel(Convert.ToInt32(ds.Tables[0].Rows[0]["id"].ToString()));
            }
            return flag;
        }
        #endregion
        public Login()
        {
            InitializeComponent();
        }

        private void DoLogin()
        {
            if (CheckLogin() == true)
            {
                MainFrm mainFrm = new MainFrm();
                Func.user = user;
                this.Hide();
                mainFrm.Show();
            }
            else
            {
                MessageBox.Show(this, "用户名或密码错误！");
                txtUserName.Focus();
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            DoLogin();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                DoLogin();
            }
        }

    }
}
